package com.yyfax.ph.personal.bindapply;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.List;

/**
 * 类描述： 获取绑定申请的子账户列表
 */
public class BindApplyModel extends Model {
	private static final String TAG = BindApplyModel.class.getSimpleName();

	public static final int MSG_GET_BIND_ACCOUNTS = 148;
	public static final int MSG_HANDLE_BIND = 149;

	//0：申请中 1：已接受 2:已拒绝 3:已失效
	public static final String STATUS_PROCESS = "0";
	public static final String STATUS_PASS = "1";
	public static final String STATUS_REFUSE = "2";
	public static final String STATUS_EXPIRED = "3";



	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case MSG_GET_BIND_ACCOUNTS:
				return service.getApplyList(token, (ApplyList) params);
			case MSG_HANDLE_BIND:
				return service.handle(token, (Handle) params);
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case MSG_GET_BIND_ACCOUNTS:
				ApplyListResult applyListResult = cast(obj);
				EventAgent.post(applyListResult);
				break;
			case MSG_HANDLE_BIND:
				HandleResult handleResult = cast(obj);
				EventAgent.post(handleResult);
				break;
		}
	}

	public interface Service {
		@POST("account/bind/getApplyList")
		Observable<ApplyListResult> getApplyList(@Header("token") String token, @Body ApplyList applyList);

		@POST("account/bind/handle")
		Observable<HandleResult> handle(@Header("token") String token, @Body Handle handle);
	}

	static class ApplyList {
		String pageNo;
		String pageSize;

		public ApplyList(String pageNo, String pageSize) {
			this.pageNo = pageNo;
			this.pageSize = pageSize;
		}
	}

	public static class ApplyListBean {
		public String applyCount;
		public String applyTime;
		public String bindTime;   //暂时用不到
		public String userId;
		public String id;
		public String phone;
		public String status;
		public String statusName;
	}


	static class ApplyListResult extends BaseResultEntity<List<ApplyListBean>> {
	}

	static class Handle {
		String id;
		String type;

		public Handle(String id, String type) {
			this.id = id;
			this.type = type;
		}
	}

	static class HandleResult extends BaseResultEntity {
	}
}
